<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="用户编号" prop="phoneNo">
        <el-input
          v-model="queryParams.phoneNo"
          placeholder="请输入用户编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址编号" prop="terminalNo">
        <el-input
          v-model="queryParams.terminalNo"
          placeholder="请输入地址编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌名称" prop="smName">
        <el-input
          v-model="queryParams.smName"
          placeholder="请输入品牌名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户状态" prop="runName">
        <el-select
          v-model="queryParams.runName"
          placeholder="请选择用户状态"
          clearable
          style="width: 160px"
        >
          <el-option label="活跃" value="活跃" />
          <el-option label="非活跃" value="非活跃" />
          <el-option label="暂停" value="暂停" />
          <el-option label="注销" value="注销" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户等级" prop="ownerName">
        <el-select
          v-model="queryParams.ownerName"
          placeholder="请选择用户等级"
          clearable
          style="width: 160px"
        >
          <el-option label="普通用户" value="普通用户" />
          <el-option label="VIP用户" value="VIP用户" />
          <el-option label="钻石用户" value="钻石用户" />
        </el-select>
      </el-form-item>
      <el-form-item label="小区地址" prop="estateName">
        <el-input
          v-model="queryParams.estateName"
          placeholder="请输入小区地址"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      <el-button type="info" icon="el-icon-export" size="mini" @click="handleExport">导出</el-button>
    </el-form>

    <!-- 标签筛选区域 -->
    <el-form :model="queryParams" ref="labelForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="业务品牌" prop="businessBrand">
        <el-select
          v-model="queryParams.businessBrand"
          placeholder="请选择业务品牌"
          clearable
          style="width: 160px"
        >
          <el-option label="有线电视" value="有线电视" />
          <el-option label="高清互动" value="高清互动" />
          <el-option label="宽带业务" value="宽带业务" />
          <el-option label="融合套餐" value="融合套餐" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户价值等级" prop="userValueLevel">
        <el-select
          v-model="queryParams.userValueLevel"
          placeholder="请选择用户价值等级"
          clearable
          style="width: 160px"
        >
          <el-option label="高价值用户" value="高价值用户" />
          <el-option label="中价值用户" value="中价值用户" />
          <el-option label="低价值用户" value="低价值用户" />
        </el-select>
      </el-form-item>
      <el-form-item label="电视消费水平" prop="tvConsumeLevel">
        <el-select
          v-model="queryParams.tvConsumeLevel"
          placeholder="请选择电视消费水平"
          clearable
          style="width: 160px"
        >
          <el-option label="低" value="低" />
          <el-option label="中" value="中" />
          <el-option label="高" value="高" />
          <el-option label="非常高" value="非常高" />
        </el-select>
      </el-form-item>
      <el-form-item label="宽带消费水平" prop="broadbandConsumeLevel">
        <el-select
          v-model="queryParams.broadbandConsumeLevel"
          placeholder="请选择宽带消费水平"
          clearable
          style="width: 160px"
        >
          <el-option label="低" value="低" />
          <el-option label="中" value="中" />
          <el-option label="高" value="高" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户忠诚度" prop="userLoyalty">
        <el-select
          v-model="queryParams.userLoyalty"
          placeholder="请选择用户忠诚度"
          clearable
          style="width: 160px"
        >
          <el-option label="高忠诚度" value="高忠诚度" />
          <el-option label="中忠诚度" value="中忠诚度" />
          <el-option label="低忠诚度" value="低忠诚度" />
        </el-select>
      </el-form-item>
      <el-form-item label="套餐类型" prop="packageType">
        <el-select
          v-model="queryParams.packageType"
          placeholder="请选择套餐类型"
          clearable
          style="width: 160px"
        >
          <el-option label="基础套餐" value="基础套餐" />
          <el-option label="标准套餐" value="标准套餐" />
          <el-option label="高级套餐" value="高级套餐" />
          <el-option label="尊享套餐" value="尊享套餐" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否待挽留" prop="isRetention">
        <el-select
          v-model="queryParams.isRetention"
          placeholder="请选择是否待挽留"
          clearable
          style="width: 160px"
        >
          <el-option label="是" value="是" />
          <el-option label="否" value="否" />
        </el-select>
      </el-form-item>
      <el-button type="success" icon="el-icon-filter" size="mini" @click="handleQuery">筛选</el-button>
    </el-form>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
      row-key="userId"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" prop="userId" width="80" />
      <el-table-column label="用户编号" prop="phoneNo" width="140" :show-overflow-tooltip="true" />
      <el-table-column label="地址编号" prop="terminalNo" width="140" :show-overflow-tooltip="true" />
      <el-table-column label="品牌名称" prop="smName" width="100" />
      <el-table-column label="状态名称" prop="runName" width="100">
        <template slot-scope="scope">
          <el-tag :type="getRunNameType(scope.row.runName)">{{ scope.row.runName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户等级" prop="ownerName" width="100" />
      <el-table-column label="小区地址" prop="estateName" :show-overflow-tooltip="true" />
      <el-table-column label="开户时间" prop="openTime" width="120" />
      <el-table-column label="宽带状态" prop="force" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.force === '是' ? 'success' : 'danger'">{{ scope.row.force }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="业务品牌" prop="businessBrand" width="120">
        <template slot-scope="scope">
          <el-tag :type="getBusinessBrandType(scope.row.businessBrand)">{{ scope.row.businessBrand }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户价值等级" prop="userValueLevel" width="120">
        <template slot-scope="scope">
          <el-tag :type="getUserValueLevelType(scope.row.userValueLevel)">{{ scope.row.userValueLevel }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="电视消费水平" prop="tvConsumeLevel" width="120" />
      <el-table-column label="宽带消费水平" prop="broadbandConsumeLevel" width="120" />
      <el-table-column label="用户忠诚度" prop="userLoyalty" width="100" />
      <el-table-column label="套餐类型" prop="packageType" width="100" />
      <el-table-column label="是否待挽留" prop="isRetention" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isRetention === '是' ? 'warning' : 'info'">{{ scope.row.isRetention }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="180" />
      <el-table-column label="操作" width="130" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-permis="['media:user:edit']"
            type="primary" 
            size="mini"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            v-permis="['media:user:remove']"
            type="danger" 
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广电用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户编号" prop="phoneNo">
              <el-input v-model="form.phoneNo" placeholder="请输入用户编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址编号" prop="terminalNo">
              <el-input v-model="form.terminalNo" placeholder="请输入地址编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="品牌名称" prop="smName">
              <el-input v-model="form.smName" placeholder="请输入品牌名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌编号" prop="smCode">
              <el-input v-model="form.smCode" placeholder="请输入品牌编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态名称" prop="runName">
              <el-select v-model="form.runName" placeholder="请选择状态名称">
                <el-option label="活跃" value="活跃" />
                <el-option label="非活跃" value="非活跃" />
                <el-option label="暂停" value="暂停" />
                <el-option label="注销" value="注销" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户等级名称" prop="ownerName">
              <el-select v-model="form.ownerName" placeholder="请选择用户等级名称">
                <el-option label="普通用户" value="普通用户" />
                <el-option label="VIP用户" value="VIP用户" />
                <el-option label="钻石用户" value="钻石用户" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户等级编号" prop="ownerCode">
              <el-input v-model="form.ownerCode" placeholder="请输入用户等级编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户时间" prop="openTime">
              <el-date-picker
                v-model="form.openTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择开户时间"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="完整地址" prop="addressoj">
              <el-input v-model="form.addressoj" placeholder="请输入完整地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小区地址" prop="estateName">
              <el-input v-model="form.estateName" placeholder="请输入小区地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="宽带是否生效" prop="force">
              <el-radio-group v-model="form.force">
                <el-radio label="是">是</el-radio>
                <el-radio label="否">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否待挽留" prop="isRetention">
              <el-radio-group v-model="form.isRetention">
                <el-radio label="是">是</el-radio>
                <el-radio label="否">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider>标签信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="业务品牌" prop="businessBrand">
              <el-select v-model="form.businessBrand" placeholder="请选择业务品牌">
                <el-option label="有线电视" value="有线电视" />
                <el-option label="高清互动" value="高清互动" />
                <el-option label="宽带业务" value="宽带业务" />
                <el-option label="融合套餐" value="融合套餐" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户价值等级" prop="userValueLevel">
              <el-select v-model="form.userValueLevel" placeholder="请选择用户价值等级">
                <el-option label="高价值用户" value="高价值用户" />
                <el-option label="中价值用户" value="中价值用户" />
                <el-option label="低价值用户" value="低价值用户" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="电视消费水平" prop="tvConsumeLevel">
              <el-select v-model="form.tvConsumeLevel" placeholder="请选择电视消费水平">
                <el-option label="低" value="低" />
                <el-option label="中" value="中" />
                <el-option label="高" value="高" />
                <el-option label="非常高" value="非常高" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宽带消费水平" prop="broadbandConsumeLevel">
              <el-select v-model="form.broadbandConsumeLevel" placeholder="请选择宽带消费水平">
                <el-option label="低" value="低" />
                <el-option label="中" value="中" />
                <el-option label="高" value="高" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户忠诚度" prop="userLoyalty">
              <el-select v-model="form.userLoyalty" placeholder="请选择用户忠诚度">
                <el-option label="高忠诚度" value="高忠诚度" />
                <el-option label="中忠诚度" value="中忠诚度" />
                <el-option label="低忠诚度" value="低忠诚度" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐类型" prop="packageType">
              <el-select v-model="form.packageType" placeholder="请选择套餐类型">
                <el-option label="基础套餐" value="基础套餐" />
                <el-option label="标准套餐" value="标准套餐" />
                <el-option label="高级套餐" value="高级套餐" />
                <el-option label="尊享套餐" value="尊享套餐" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, addUser, updateUser, delUser, exportUser } from '@/api/media/user'

export default {
  name: 'MediaUser',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      // 广电用户表格数据
      userList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phoneNo: undefined,
        terminalNo: undefined,
        smName: undefined,
        runName: undefined,
        ownerName: undefined,
        estateName: undefined,
        businessBrand: undefined,
        userValueLevel: undefined,
        tvConsumeLevel: undefined,
        broadbandConsumeLevel: undefined,
        userLoyalty: undefined,
        packageType: undefined,
        isRetention: undefined
      },
      // 表单参数
      form: {
        userId: undefined,
        phoneNo: undefined,
        terminalNo: undefined,
        smName: undefined,
        smCode: undefined,
        runName: undefined,
        ownerName: undefined,
        ownerCode: undefined,
        runTime: undefined,
        addressoj: undefined,
        estateName: undefined,
        openTime: undefined,
        force: '否',
        businessBrand: undefined,
        userValueLevel: undefined,
        tvConsumeLevel: undefined,
        broadbandConsumeLevel: undefined,
        userLoyalty: undefined,
        packageType: undefined,
        isRetention: '否'
      },
      // 表单校验
      rules: {
        phoneNo: [
          { required: true, message: '用户编号不能为空', trigger: 'blur' }
        ],
        terminalNo: [
          { required: true, message: '地址编号不能为空', trigger: 'blur' }
        ],
        smName: [
          { required: true, message: '品牌名称不能为空', trigger: 'blur' }
        ],
        runName: [
          { required: true, message: '状态名称不能为空', trigger: 'change' }
        ],
        ownerName: [
          { required: true, message: '用户等级名称不能为空', trigger: 'change' }
        ],
        openTime: [
          { required: true, message: '开户时间不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 获取列表 */
    getList() {
      this.loading = true
      listUser(this.queryParams).then(res => {
        this.userList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.resetForm('labelForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增广电用户'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const userId = row.userId || this.ids
      getUser(userId).then(res => {
        this.form = res.data
        this.open = true
        this.title = '修改广电用户'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addUser(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 重置表单 */
    reset() {
      this.form = {
        userId: undefined,
        phoneNo: undefined,
        terminalNo: undefined,
        smName: undefined,
        smCode: undefined,
        runName: undefined,
        ownerName: undefined,
        ownerCode: undefined,
        runTime: undefined,
        addressoj: undefined,
        estateName: undefined,
        openTime: undefined,
        force: '否',
        businessBrand: undefined,
        userValueLevel: undefined,
        tvConsumeLevel: undefined,
        broadbandConsumeLevel: undefined,
        userLoyalty: undefined,
        packageType: undefined,
        isRetention: '否'
      }
      this.resetForm('form')
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.download('media/user/export', { ...queryParams }, `media-user_${new Date().getTime()}.xlsx`)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids
      this.$confirm(
        `确定要删除选中的广电用户吗？`,
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(function() {
        return delUser(userIds)
      }).then(res => {
        if (res.code === 200) {
          this.msgSuccess('删除成功')
          this.getList()
        }
      }).catch(() => {})
    },
    /** 获取用户状态标签类型 */
    getRunNameType(runName) {
      switch (runName) {
        case '活跃':
          return 'success'
        case '非活跃':
          return 'info'
        case '暂停':
          return 'warning'
        case '注销':
          return 'danger'
        default:
          return 'info'
      }
    },
    /** 获取业务品牌标签类型 */
    getBusinessBrandType(businessBrand) {
      switch (businessBrand) {
        case '有线电视':
          return 'primary'
        case '高清互动':
          return 'success'
        case '宽带业务':
          return 'warning'
        case '融合套餐':
          return 'info'
        default:
          return 'info'
      }
    },
    /** 获取用户价值等级标签类型 */
    getUserValueLevelType(userValueLevel) {
      switch (userValueLevel) {
        case '高价值用户':
          return 'success'
        case '中价值用户':
          return 'warning'
        case '低价值用户':
          return 'info'
        default:
          return 'info'
      }
    }
  }
}
</script>